package com.million.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.million.eduservice.entity.EduChapter;
import com.million.eduservice.entity.EduVideo;
import com.million.eduservice.entity.vo.chapter.ChapterVo;
import com.million.eduservice.entity.vo.chapter.VideoVo;
import com.million.eduservice.mapper.EduChapterMapper;
import com.million.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.eduservice.service.EduVideoService;
import com.million.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-03-24
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;  //方便查询video

    //获取章节和小节的信息
    @Override
    public List<ChapterVo> getChapterVideoByCourseId (String courseId) {

        //1.通过课程id  查询到对应的章节，用list集合来表示
        ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>(); //返回的结果
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        //按照sort升序排序
        queryWrapper.orderByAsc("sort");
        List<EduChapter> eduChapterList = baseMapper.selectList(queryWrapper);


        //2.通过课程id或者章节id查询到对应的小节，用list集合来表示
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id", courseId);
        //按照sort升序排序
        queryWrapper1.orderByAsc("sort");
        List<EduVideo> eduVideoList = eduVideoService.list(queryWrapper1);

        //3.遍历集合，填充章节数据，进行数据封装
        int countChapter = eduChapterList.size();
        for (int i = 0; i < countChapter; i++) {
            //得到每一个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //将eduChapter赋值给ChapterVo
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);

            //4.遍历集合查询小节数据，进行判断封装
            int countVideo = eduVideoList.size();
            ArrayList<VideoVo> videoVos = new ArrayList<>();
            for (int j = 0; j < countVideo; j++) {
                //得到每一个小节
                EduVideo eduVideo = eduVideoList.get(j);
                //判断小节里面的chapter_id 和章节的id是否一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVo.setVideoSourceId(eduVideo.getVideoSourceId());
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
            chapterVoArrayList.add(chapterVo);
        }
        return chapterVoArrayList;
    }

    //删除章节方法
    @Override
    public boolean deleteChapter (String chapterId) {
        //根据章节下面是否有小节判断是否删除，有=》不删
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(queryWrapper);
        //如果count 大于>0  表示有值
        if (count > 0) {
            throw new GuliException(20001, "有小节内容，无法删除");
        } else {
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    //修改章节方法
    @Override
    public void updateChapter (ChapterVo chapterVo) {
        EduChapter eduChapter = new EduChapter();
        BeanUtils.copyProperties(chapterVo, eduChapter);
        int i = baseMapper.updateById(eduChapter);
        if (i == 0) {
            throw new GuliException(20001, "修改失败");
        }
    }

    //根据课程id 删除章节信息
    @Override
    @Transactional  //添加事务
    public void removeChapterById (String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        baseMapper.delete(queryWrapper);
    }
}
